$(function () {
    $("#page").load("/back/page.html");
    findAll();

})

function findAll() {
    let da = {};
    da.username = $.trim($("#name").val());
    da.startBirthday = $("#startBirthday").val();
    da.endBirthday = $("#endBirthday").val();
    da.introduce = $.trim($("#introduce").val());
    da.hobby = JSON.stringify(aihao());
    da.sex = $("#sex").find("option:selected").text();
    da.phone = $.trim($("#phone").val());
    da.pageSize = $.trim($("#pageSize").val());
    da.pageNum = $.trim($("#pageNum").val());
    console.log(da);
    let data = myAjax("/back/user/findAll", da, "get");
    let html = '';
    //总条数
    $("#count").html(data.count);
    data = data.list;
    if (data != null) {
        for (let i = 0; i < data.length; i++) {
            html += ' <div><div style="width: 50px">' + data[i].id + '</div>\n' +
                '        <div style="width: 150px">' + data[i].username + '</div>\n' +
                '        <div style="width: 100px">' + data[i].birthday + '</div>\n' +
                '        <div style="width: 100px">' + data[i].introduce + '</div>\n' +
                '        <div style="width: 200px">' + data[i].hobby + '</div>\n' +
                '        <div style="width: 50px">' + data[i].sex + '</div>\n' +
                '        <div style="width: 150px">' + data[i].phone + '</div>\n' +
                '            <div style="width: 50px">\n' +
                '                <div><a href="javascript:edit(' + data[i].id + ')" >编辑</a></div>\n' +
                '                <div><a href="javascript:del(' + data[i].id + ')" >删除</a></div>\n' +
                '            </div>\n' +
                '        </div>';
        }
    }

    $("#userFindAll").html(html);
}

function edit(id) {
    localStorage.setItem("userid", id);
    sessionStorage.setItem("userid", id);
    $('#right').load('/back/user/UserEdit.html')
}

function del(id) {
    let data = myAjax("/back/user/del", {id: id}, "post");
    if (data.count == 1) {
        findAll();
    }
}

function aihao() {
    let hb = [];
    $("input:checkbox[name='hobby']").each(function () {
        console.log();
        if ($(this).prop("checked")) {
            hb.push($(this).val());
        }
    })

    return hb;
}

